package com.atguigu.srb.core.controller.admin;

import com.atguigu.srb.core.exception.Assert;
import com.atguigu.srb.core.pojo.entity.UserInfo;
import com.atguigu.srb.core.pojo.query.UserInfoQuery;
import com.atguigu.srb.core.result.R;
import com.atguigu.srb.core.result.ResponseEnum;
import com.atguigu.srb.core.service.UserInfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

//@CrossOrigin
@RestController
@Api(tags = "会员管理")
@RequestMapping("/admin/core/userInfo")
public class AdminUserInfoController {

    @Autowired
    private UserInfoService userInfoService;


    @ApiOperation("会员的锁定和解锁")
    @PutMapping("/lock/{id}/{status}")
    public Object lock(@PathVariable("id") @ApiParam(value = "会员的id", required = true, example = "1") Long id,
                       @PathVariable("status") @ApiParam(value = "会员的状态状（0：锁定 1：正常）", defaultValue = "1", required = true) Integer status) {
        //@ApiModelProperty(value = "状态（0：锁定 1：正常）")
        //private Integer status;
        UserInfo userInfo = null;

        // 参数校验

        // 根据id进行查询医院设置的信息
        userInfo = userInfoService.getById(id);
        if (ObjectUtils.isEmpty(userInfo) || userInfo.getStatus() == status) {
            return R.error().message("fail");
        }
        // 设置状态
        userInfo.setStatus(status);
        // 调用业务层修改方法
        boolean res = userInfoService.updateById(userInfo);
        // 返回结果
        //return R.ok().message(res ? "已经锁定" : "正常");
        return R.ok().data("状态已经修改", res);
    }

    @ApiOperation("会员分页查询")
    @GetMapping("/listPage/{page}/{limit}")
    public Object listPage(@PathVariable("page") @ApiParam(value = "当前页", required = true) Long page,
                           @PathVariable("limit") @ApiParam(value = "每页条数", required = true) Long limit,
                           @RequestParam(required = false) UserInfoQuery UserInfoQuery
    ) {
        String mobile = "";
        Integer status = null;
        Integer userType = null;
        if (!ObjectUtils.isEmpty(UserInfoQuery)) {
            // 参数获取
            mobile = UserInfoQuery.getMobile();
            status = UserInfoQuery.getStatus();
            userType = UserInfoQuery.getUserType();
        }
        // 参数校验
        if (page < 0 || limit < 0) {
            return R.error();
        }

        // 条件查询封装,
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(!StringUtils.isEmpty(mobile), "mobile", mobile);
        queryWrapper.eq(status != null, "status", status);
        queryWrapper.eq(userType != null, "user_type", userType);

        // 创建分页对象, 调用查询方法
        Page<UserInfo> userInfoPage = new Page<>(page, limit);
        userInfoPage = userInfoService.page(userInfoPage, queryWrapper);
        // 返回数据
        return R.ok().data("userInfoPage", userInfoPage);
    }


}
